package com.pounds.service.impl;

import com.pounds.mapper.ChargeMapper;
import com.pounds.mapper.UserMapper;
import com.pounds.pojo.Charge;
import com.pounds.service.ChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChargeServiceImpl implements ChargeService {

    @Autowired
    private ChargeMapper chargeMapper;

    @Autowired
    private UserMapper userMapper;

    enum ChargeEnumError {
        Normal,
        DataFormatError,
        EmptyError,
        DataNotFoundError
    }

    @Override
    public List<Charge> getChargeList() {
        return chargeMapper.getChargeList();
    }

    @Override
    public String addCharge(int user, String enter, String come, String price, String need, String note) {
        if (enter == null || come == null || price == null || user < 0) {
            return ChargeEnumError.DataFormatError.toString();
        } else if (enter.isEmpty() || come.isEmpty() || price.isEmpty()) {
            return ChargeEnumError.EmptyError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return ChargeEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return ChargeEnumError.DataNotFoundError.toString();
        }
        chargeMapper.addCharge(user, name, enter, come, price, need, note);
        return ChargeEnumError.Normal.toString();
    }

    @Override
    public String deleteCharge(int id) {
        List<Charge> charge = chargeMapper.selectCharge(id);
        if (charge == null) {
            return ChargeEnumError.DataNotFoundError.toString();
        }
        chargeMapper.deleteCharge(id);
        return ChargeEnumError.Normal.toString();
    }

    @Override
    public String updateCharge(int id, int user, String enter, String come, String price, String need, String note) {
        if (enter == null || come == null || price == null || user < 0 || id < 0) {
            return ChargeEnumError.DataFormatError.toString();
        } else if (enter.isEmpty() || come.isEmpty() || price.isEmpty()) {
            return ChargeEnumError.EmptyError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return ChargeEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return ChargeEnumError.DataNotFoundError.toString();
        }
        chargeMapper.updateCharge(id, user, name, enter, come, price, need, note);
        return ChargeEnumError.Normal.toString();
    }
}
